package cn.niit.letuseatdemo.controller.admin;

import cn.niit.letuseatdemo.entity.domain.Apeal;
import cn.niit.letuseatdemo.service.ApealService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @Description:
 * @Author: zhang_bowen
 * @CreateTime： 2022/12/7 22:11
 */
@Slf4j
@RequestMapping("/admin/user")
@Controller
public class AdminUserAppealController {

    @Autowired
    private ApealService apealService;

    /*
      * @Description: 管理员查询用户申诉列表页面初始化及查询
      * @Author: zhang_bowen
      * @CreateTime: 2022/12/7  22:48
 * @return: null
      **/

    @GetMapping("")
    public String index(Model model, Apeal apeal){
        log.info("用户申诉列表查询的搜索条件是：" + apeal);
        model.addAttribute("apeal",apeal);
        List<Apeal> adminApealList = apealService.selectAdminApealList(apeal);

        //开启分页查询
        System.out.println(adminApealList);
        PageInfo<Apeal> pageInfo = new PageInfo<>(adminApealList);
        log.info("分页对象 = {}",pageInfo);
        log.info("查询出来的用户申诉列表：{}",adminApealList);
        model.addAttribute("adminApealList",adminApealList);
        model.addAttribute("pageInfo",pageInfo);
        return "admin/user/list";
    }

    /*
      * @Description: 初始化审核页面
      * @Author: zhang_bowen
      * @CreateTime: 2022/12/8  20:15
 * @return: null
      **/
    @GetMapping("/{apealId}")
    public String saveAdminApealIndex(Model model,@PathVariable("apealId")Integer apealId){
        log.info("要修改的Id = " +apealId);
        Apeal apeal = null;
        if(null != apealId && apealId > 0){
            apeal = apealService.selectAdminApealByApealId(apealId);
        }else {
            apeal = new Apeal();
        }
        model.addAttribute("apeal",apeal);
        return "/admin/user/add";
    }

    /*
     * @Description: 根据ID修改审核状态（通过）
     * @Author: zhang_bowen
     * @CreateTime: 2022/12/3  18:02
     * @return: null
     **/
    @GetMapping("/auditSuccess/{id}")
    public String ediAdminSuccessSave(@PathVariable("id") Integer id){

        int rows = apealService.auditSuccess(id);
        if(rows <= 0){//修改失败
            return "/admin/user/add";
        }
        return "redirect:/admin/user";
    }

    @GetMapping("/auditFail/{id}")
    public String ediAdminFailSave(@PathVariable("id") Integer id){

        int rows = apealService.auditFail(id);
        if(rows <= 0){//修改失败
            return "/admin/user/add";
        }
        return "redirect:/admin/user";
    }

}